<template>
  <div class="page-container">
    <SearchPanel @change="handleSearchPanelChange">
      <el-form
        class="query-more-form"
        :model="queryParams"
        ref="queryForm"
        size="mini"
        label-width="110px"
      >
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="工单编号" prop="number">
              <el-input
                v-model="queryParams.number"
                placeholder="请输入工单编号"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标题" prop="title">
              <el-input
                v-model="queryParams.title"
                placeholder="请输入标题"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="工单类型" prop="typeId">
              <el-input
                v-model="queryParams.typeId"
                placeholder="请输入工单类型"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="市" prop="addressCityCode">
              <el-input
                v-model="queryParams.addressCityCode"
                placeholder="请输入行政地址-市"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="街道" prop="addressStreetCode">
              <el-input
                v-model="queryParams.addressStreetCode"
                placeholder="请输入行政地址-街道"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="社区" prop="addressCommunityCode">
              <el-input
                v-model="queryParams.addressCommunityCode"
                placeholder="请输入行政地址-社区"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="所属网格" prop="areaId">
              <el-input
                v-model="queryParams.areaId"
                placeholder="请输入所属网格"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="所属行业编码" prop="categoryDcaCode">
              <el-input
                v-model="queryParams.categoryDcaCode"
                placeholder="请输入所属行业编码"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="主类别编码" prop="categorySupCode">
              <el-input
                v-model="queryParams.categorySupCode"
                placeholder="请输入主类别编码"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="子类别编码" prop="categorySubCode">
              <el-input
                v-model="queryParams.categorySubCode"
                placeholder="请输入子类别编码"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="办理部门" prop="deptId">
              <el-input
                v-model="queryParams.deptId"
                placeholder="请输入办理部门"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                size="mini"
                @click="handleQuery"
                >搜索</el-button
              >
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
                >重置</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </SearchPanel>
    <div class="table-container">
      <div class="table-tools-panel table-flex-row">
        <el-button class="flush-btn" size="mini" icon="el-icon-refresh"
          >刷新</el-button
        >
      </div>
      <div class="table-list-panel">
        <el-table
          v-loading="loading"
          :data="orderList"
          border
          size="small"
          :height="isShow ? 'calc(100vh - 500px)' : 'calc(100vh - 405px)'"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="工单编号" align="center" prop="number" />
          <el-table-column label="标题" align="center" prop="title" />
          <el-table-column label="工单类型值" align="center" prop="typeValue" />
          <el-table-column
            label="工单地址"
            align="center"
            prop="address"
            show-overflow-tooltip
          />
          <el-table-column
            label="行政地址-市"
            align="center"
            prop="addressCityName"
          />
          <el-table-column
            label="行政地址-街道"
            align="center"
            prop="addressStreetName"
          />
          <el-table-column
            label="行政地址-社区"
            align="center"
            prop="addressCommunityName"
          />
          <el-table-column label="网格名称" align="center" prop="areaName" />
          <el-table-column
            label="所属行业"
            align="center"
            prop="categoryDcaName"
          />
          <el-table-column
            label="主类别"
            align="center"
            prop="categorySupName"
          />
          <el-table-column
            label="子类别"
            align="center"
            prop="categorySubName"
            show-overflow-tooltip
          />
          <el-table-column label="办理部门值" align="center" prop="deptValue" />
          <el-table-column label="办理人名称" align="center" prop="userValue" />
          <el-table-column
            label="限制办结时间"
            align="center"
            prop="astrictTime"
            width="160"
          >
            <template slot-scope="scope">
              <span>{{
                parseTime(scope.row.astrictTime, "{y}-{m}-{d} {h}:{m}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="实际办结时间"
            align="center"
            prop="actualTime"
            width="160"
          >
            <template slot-scope="scope">
              <span>{{
                parseTime(scope.row.actualTime, "{y}-{m}-{d} {h}:{m}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="是否超时" align="center" prop="overtime" />
          <el-table-column label="操作" width="120" fixed="right">
            <template slot-scope="scope">
              <el-button class="table-inner-btn" type="text" size="mini"
                >详情</el-button
              >
              <el-button
                class="table-inner-btn"
                type="text"
                size="mini"
                @click="selectFlow(scope.row.number)"
                >流程</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-panel">
        <Pagination
          :total="queryParams.total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工单编号" prop="number">
          <el-input v-model="form.number" placeholder="请输入工单编号" />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="工单类型id" prop="typeId">
          <el-input v-model="form.typeId" placeholder="请输入工单类型id" />
        </el-form-item>
        <el-form-item label="工单类型值" prop="typeValue">
          <el-input v-model="form.typeValue" placeholder="请输入工单类型值" />
        </el-form-item>
        <el-form-item label="工单地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入工单地址" />
        </el-form-item>
        <el-form-item label="行政地址-市" prop="addressCityCode">
          <el-input
            v-model="form.addressCityCode"
            placeholder="请输入行政地址-市"
          />
        </el-form-item>
        <el-form-item label="行政地址-市" prop="addressCityName">
          <el-input
            v-model="form.addressCityName"
            placeholder="请输入行政地址-市"
          />
        </el-form-item>
        <el-form-item label="行政地址-街道" prop="addressStreetCode">
          <el-input
            v-model="form.addressStreetCode"
            placeholder="请输入行政地址-街道"
          />
        </el-form-item>
        <el-form-item label="行政地址-街道" prop="addressStreetName">
          <el-input
            v-model="form.addressStreetName"
            placeholder="请输入行政地址-街道"
          />
        </el-form-item>
        <el-form-item label="行政地址-社区" prop="addressCommunityCode">
          <el-input
            v-model="form.addressCommunityCode"
            placeholder="请输入行政地址-社区"
          />
        </el-form-item>
        <el-form-item label="行政地址-社区" prop="addressCommunityName">
          <el-input
            v-model="form.addressCommunityName"
            placeholder="请输入行政地址-社区"
          />
        </el-form-item>
        <el-form-item label="所属网格" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入所属网格" />
        </el-form-item>
        <el-form-item label="网格名称" prop="areaName">
          <el-input v-model="form.areaName" placeholder="请输入网格名称" />
        </el-form-item>
        <el-form-item label="所属行业编码" prop="categoryDcaCode">
          <el-input
            v-model="form.categoryDcaCode"
            placeholder="请输入所属行业编码"
          />
        </el-form-item>
        <el-form-item label="所属行业名称" prop="categoryDcaName">
          <el-input
            v-model="form.categoryDcaName"
            placeholder="请输入所属行业名称"
          />
        </el-form-item>
        <el-form-item label="主类别编码" prop="categorySupCode">
          <el-input
            v-model="form.categorySupCode"
            placeholder="请输入主类别编码"
          />
        </el-form-item>
        <el-form-item label="主类别名称" prop="categorySupName">
          <el-input
            v-model="form.categorySupName"
            placeholder="请输入主类别名称"
          />
        </el-form-item>
        <el-form-item label="子类别编码" prop="categorySubCode">
          <el-input
            v-model="form.categorySubCode"
            placeholder="请输入子类别编码"
          />
        </el-form-item>
        <el-form-item label="子类别名称" prop="categorySubName">
          <el-input
            v-model="form.categorySubName"
            placeholder="请输入子类别名称"
          />
        </el-form-item>
        <el-form-item label="办理部门" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入办理部门" />
        </el-form-item>
        <el-form-item label="办理部门值" prop="deptValue">
          <el-input v-model="form.deptValue" placeholder="请输入办理部门值" />
        </el-form-item>
        <el-form-item label="办理人" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入办理人" />
        </el-form-item>
        <el-form-item label="办理人名称" prop="userValue">
          <el-input v-model="form.userValue" placeholder="请输入办理人名称" />
        </el-form-item>
        <el-form-item label="限制办结时间" prop="astrictTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.astrictTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择限制办结时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实际办结时间" prop="actualTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.actualTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择实际办结时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否超时" prop="overtime">
          <el-input v-model="form.overtime" placeholder="请输入是否超时" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <WorkOrderFlowDialog
      :dialogVisible="flowDialogVisible"
      @closeDialog="flowDialogVisible = false"
      :number="number"
    />
  </div>
</template>

<script>
import SearchPanel from "@/components/SearchPanel";
import Pagination from "@/components/Pagination";
import { parseTime } from "@/utils/index";
import WorkOrderFlowDialog from "./components/WorkOrderFlowDialog";
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
  exportOrder,
} from "@/api/wfm/workOrder";

export default {
  name: "WorkOrder",
  components: { SearchPanel, Pagination, WorkOrderFlowDialog },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 【请填写功能名称】表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
        number: undefined,
        title: undefined,
        typeId: undefined,
        address: undefined,
        addressCityCode: undefined,
        addressStreetCode: undefined,
        addressCommunityCode: undefined,
        areaId: undefined,
        categoryDcaCode: undefined,
        categorySupCode: undefined,
        categorySubCode: undefined,
        deptId: undefined,
        userId: undefined,
        astrictTime: undefined,
        actualTime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      isShow: true,
      // 流程模态框
      flowDialogVisible: false,
      // 工单编号
      number: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    handleSearchPanelChange(flag) {
      this.isShow = flag;
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then((response) => {
        this.orderList = response.rows;
        this.queryParams.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        number: undefined,
        title: undefined,
        createTime: undefined,
        typeId: undefined,
        typeValue: undefined,
        address: undefined,
        addressCityCode: undefined,
        addressCityName: undefined,
        addressStreetCode: undefined,
        addressStreetName: undefined,
        addressCommunityCode: undefined,
        addressCommunityName: undefined,
        areaId: undefined,
        areaName: undefined,
        categoryDcaCode: undefined,
        categoryDcaName: undefined,
        categorySupCode: undefined,
        categorySupName: undefined,
        categorySubCode: undefined,
        categorySubName: undefined,
        deptId: undefined,
        deptValue: undefined,
        userId: undefined,
        userValue: undefined,
        astrictTime: undefined,
        actualTime: undefined,
        overtime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 查看流程
    selectFlow(number) {
      this.flowDialogVisible = true;
      this.number = number;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOrder(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delOrder(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有【请填写功能名称】数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportOrder(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
